local attribute = {}

--属性编辑
attribute.field = {}

setmetatable(attribute, attribute)

local mt = {}

attribute.__index = mt

--数据增加
function mt:add(name,value)
	if name:sub(-1, -1) == '%' then
        self:set(name,data.rate + value)
    else
        self:set(name,data.value + value)
	end
end

--数据设置
function mt:set(name,value)
	local type = 0
	if name:sub(-1, -1) == '%' then
		name = name:sub(1, -2)
		type = 1
	end
    
    local data = self.attribute_obj[name]
    if not data then
        error('[unit]错误的属性名3:' .. tostring(name))
        return
    end

	local old_value = data.value * (1 + data.rate / 100)
	local f

	if data.on_set then
        --需要对变动数据进行处理
		f = data.on_set(self, value, value - data.value)
	end

    if type==1 then
        data.rate = value
    else
        data.value = value
    end
    data.has_update = true

	if data.set then
        --对变化后的实际数据进行处理
        local new_value = data.value * (1 + data.rate / 100)
		data.set(self, new_value, old_value)
	end

	if f then
		f()
	end
end


function mt:get(name)
	local type = 0
	if name:sub(-1, -1) == '%' then
		name = name:sub(1, -2)
		type = 1
	end

    local data = self.attribute_obj[name]
    if not data then
        error('[unit]错误的属性名3:' .. tostring(name))
        return
    end

	if type == 1 then
		return data.rate
	end

    local value = data.value * (1 + data.rate / 100)
	if data.on_get then
		value = data.on_get(self,value)
	end
    if data.max then
        value = math.min(value,data.max)
    end
	return value
end

function mt:get_change()
    local data = {}
    local has = false
    for k,obj in pairs(self.attribute_obj) do
        if obj.has_update then
            obj.has_update = nil
            local value = self:get(k)

            --第一次获取不记录数值0
            if not self.init_change and value==0 then
                data[k] = nil
            else
                data[k] = value
                has = true
            end
        end
    end
    
    self.init_change = true
    return data,has
end


function mt:init_attribute()
    local handle = self.handle or self
    self.attribute_obj = {}    --数值

    --初始化
    for k,v in pairs(attribute.field) do
        local name = v.name
        local data = {}
        
        data.value = v.value or 0   --初始值
        data.rate = 0               --百分比
        data.max = v.max            --最大值

        data.get = v.get            --获取函数
        data.set = v.set            --更改函数
        data.on_get = v.on_get      --更改前处理
        data.on_set = v.on_set      --设置处理

        if data.get then
            data.value = data.get(self)
        end

        self.attribute_obj[k] = data
    end
end

--给单位绑定属性
--可以直接 设置 mt.__index = ac.unit.__index 快速设置 记得 init_attribute
function attribute.add_obj(obj)
    local data = setmetatable({}, attribute)
    data.obj = obj
    
    data:init_attribute()

    return data
end


--注册属性
function attribute.register(name,...)
    local param = {...}
    local data = attribute.field[name] or {}
    
    if #param==1 and type(param[1])=='table' then
        for k,v in pairs(param[1]) do
            data[k] = v
        end
    else
        data.value = param[1]
        data.max = param[2]
    end

    attribute.field[name] = data
end


return attribute